//
//  SAPI.m
//  TSBanPai
//
//  Created by wandong on 2017/10/17.
//  Copyright © 2017年 tingshan. All rights reserved.
//

#import "SAPI.h"

@implementation SAPI
static SAPI *_api = nil;
+(instancetype)shareInstance{
    if (!_api) {
        
        _api = [[SAPI alloc]init];
        
        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"config" ofType:@"plist"];
        _api.configDic = [[NSDictionary alloc]initWithContentsOfFile:filePath];
        _api.serverUrl1 = [_api getValue:@"serverUrl"];
        _api.imgServerUrl1 = [NSString stringWithFormat:@"%@%@",_api.serverUrl1,[_api getValue:@"imgServerUrl1"]];
#if DEBUG
        _api.testServerUrl = [_api getValue:@"testServerUrl"];
        _api.imgServerUrl1 = [NSString stringWithFormat:@"%@%@",_api.testServerUrl,[_api getValue:@"imgServerUrl1"]];
#endif
    }
    return _api;
}

- (id)getValue:(NSString*)keyname{
    if (_configDic==nil) {
        DLog(@"config.plist missed, check Config please");
        return nil;
    }else{
        if ([[_configDic allKeys] containsObject:keyname]) {
            return _configDic[keyname];
        }else{
            DLog(@"config.plist missed, check Config please");
            return @"";
        }
    }
}

- (id)getUrl:(NSString*)keyname serverUrl:(NSString*)serverUrl{
    if (_configDic==nil || _configDic[@"API"]==nil) {
        DLog(@"config.plist missed, check Config please");
        return nil;
    }else{
        if ([[_configDic[@"API"] allKeys] containsObject:keyname]) {
            return [NSString stringWithFormat:@"%@/%@", serverUrl, _configDic[@"API"][keyname]];
        }else{
            DLog(@"config.plist missed, check Config please");
            return @"";
        }
    }
}

+(NSString*)api:(NSString *)apiName{
#if DEBUG
    return [[SAPI shareInstance]getUrl:apiName serverUrl:[SAPI shareInstance].testServerUrl];
#else
    return [[SAPI shareInstance]getUrl:apiName serverUrl:[SAPI shareInstance].serverUrl1];
#endif
}

+(NSNumber*)getColor:(NSString *)color{
    return [[SAPI shareInstance] getValue:@"Color"][color];
}
@end
